var t = require("../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Component({
    properties: {
        datas: {
            type: null
        },
        component_id: {
            type: null
        }
    },
    data: {
        emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
        clientWidth: "375",
        status: 0,
        startDate: new Date("1900-01-01"),
        Height: "",
        formList: [],
        showAddress: !1,
        form_id: "",
        shoTime: "",
        types: {
            88: "diyusername",
            99: "diypassword",
            0: "diyinput",
            1: "diytextarea",
            3: "diycheckbox",
            4: "diyradio",
            2: "diyselect",
            7: "diydate",
            9: "diycity",
            5: "diyimage"
        },
        diydata: [],
        setname: "",
        uploadUrl: "upload.uploadPic",
        provinceData: [],
        cityData: [],
        districtData: [],
        pickerValue: [ 0, 0, 0 ],
        selectAddressData: [],
        currentDate: new Date().getTime(),
        showCity1: !1
    },
    lifetimes: {
        attached: function() {
            console.log(this.properties.datas);
        },
        moved: function() {},
        detached: function() {}
    },
    attached: function() {},
    ready: function() {
        this.setData({
            clientWidth: wx.getSystemInfoSync().windowWidth
        }), this._initAddressInfo(), this.data.datas.form.title && this.getForm(this.data.datas.list);
    },
    pageLifetimes: {
        show: function() {},
        hide: function() {},
        resize: function() {}
    },
    methods: {
        getForm: function(t) {
            if (this.setData({
                form_id: this.data.datas.list.id
            }), t) if (this.setData({
                status: t.status,
                loading: !0
            }), 1 === this.data.status) {
                this.setData({
                    diyform: t
                });
                var a = this.data.diyform.fields;
                for (var e in a) {
                    var i = {
                        title: a[e].tp_name,
                        value: a[e].value,
                        data_type: a[e].data_type,
                        tp_must: a[e].tp_must
                    };
                    "3" == i.data_type && (i.value = a[e].value.join(",")), "7" == i.data_type && (i.value = a[e].default_time), 
                    "1" == i.data_type && (i.value = a[e].value), this.data.formList.push(i), this.setData({
                        formList: this.data.formList
                    });
                }
            } else {
                this.setData({
                    diyform: t
                });
                var s = this.data.diyform.fields, d = this.data.diydata, n = null;
                for (var r in s) {
                    if ("diycheckbox" == this.data.types[s[r].data_type]) n = {
                        name: r,
                        data: s[r],
                        value: [],
                        type: this.data.types[s[r].data_type]
                    }; else if ("diyselect" == this.data.types[s[r].data_type]) {
                        for (var o = [], c = 0; c < s[r].tp_text.length; c++) o.push({
                            name: s[r].tp_text[c]
                        });
                        n = {
                            name: r,
                            data: s[r],
                            value: "",
                            type: this.data.types[s[r].data_type],
                            diysishow: !1,
                            sitem: o
                        };
                    } else n = "diycity" == this.data.types[s[r].data_type] ? {
                        name: r,
                        data: s[r],
                        value: "",
                        type: this.data.types[s[r].data_type],
                        showAdd: !1
                    } : "diydate" == this.data.types[s[r].data_type] ? {
                        name: r,
                        data: s[r],
                        value: "",
                        type: this.data.types[s[r].data_type],
                        timeShow: !1,
                        currentDate: new Date().getTime()
                    } : "diyimage" == this.data.types[s[r].data_type] ? {
                        name: r,
                        data: s[r],
                        imgUrls: [],
                        isPhoto: !1,
                        type: this.data.types[s[r].data_type]
                    } : {
                        name: r,
                        data: s[r],
                        value: "",
                        type: this.data.types[s[r].data_type]
                    };
                    d.push(n), this.setData({
                        diydata: d
                    });
                }
            }
        },
        getFormBack: function(t) {
            var e = this, i = a.getNetAddresss("plugin.diyform.api.diy-form.getDiyFormTypeMemberData");
            i += "&form_id=" + (t || (this.data.datas.form_id ? this.data.datas.form_id : this.data.datas.form.id)), 
            a._getNetWork({
                url: i,
                success: function(t) {
                    var a = t.data;
                    if (1 == a.result) if (e.setData({
                        status: a.data.status,
                        loading: !0
                    }), 1 === e.data.status) {
                        e.setData({
                            diyform: a.data,
                            thumb: a.data.thumb
                        });
                        var i = e.data.diyform.fields;
                        for (var s in i) {
                            var d = {
                                title: i[s].tp_name,
                                value: i[s].value,
                                data_type: i[s].data_type,
                                tp_must: i[s].tp_must
                            };
                            "3" == d.data_type && (d.value = i[s].value.join(",")), "7" == d.data_type && (d.value = i[s].default_time), 
                            "1" == d.data_type && (d.value = i[s].value), e.data.formList.push(d), e.setData({
                                formList: e.data.formList
                            });
                        }
                    } else {
                        e.setData({
                            diyform: a.data
                        });
                        var n = e.data.diyform.fields, r = e.data.diydata, o = null;
                        for (var c in n) {
                            if ("diycheckbox" == e.data.types[n[c].data_type]) o = {
                                name: c,
                                data: n[c],
                                value: [],
                                type: e.data.types[n[c].data_type]
                            }; else if ("diyselect" == e.data.types[n[c].data_type]) {
                                for (var u = [], l = 0; l < n[c].tp_text.length; l++) u.push({
                                    name: n[c].tp_text[l]
                                });
                                o = {
                                    name: c,
                                    data: n[c],
                                    value: "",
                                    type: e.data.types[n[c].data_type],
                                    diysishow: !1,
                                    sitem: u
                                };
                            } else o = "diycity" == e.data.types[n[c].data_type] ? {
                                name: c,
                                data: n[c],
                                value: "",
                                type: e.data.types[n[c].data_type],
                                showAdd: !1
                            } : "diydate" == e.data.types[n[c].data_type] ? {
                                name: c,
                                data: n[c],
                                value: "",
                                type: e.data.types[n[c].data_type],
                                timeShow: !1,
                                currentDate: new Date().getTime()
                            } : "diyimage" == e.data.types[n[c].data_type] ? {
                                name: c,
                                data: n[c],
                                imgUrls: [],
                                isPhoto: !1,
                                type: e.data.types[n[c].data_type]
                            } : {
                                name: c,
                                data: n[c],
                                value: "",
                                type: e.data.types[n[c].data_type]
                            };
                            r.push(o), e.setData({
                                diydata: r
                            });
                        }
                    }
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        tpnameinp: function(a) {
            var e = a.detail.value, i = a.currentTarget.dataset.idex;
            this.setData(t({}, "diydata[" + i + "].value", e));
        },
        onRead_1: function(t) {
            var e = t.currentTarget.dataset.name;
            this.setData({
                setname: e
            });
            var i = this, s = a.getNetAddresss(this.data.uploadUrl);
            wx.chooseImage({
                count: 1,
                sizeType: [ "original", "compressed" ],
                sourceType: [ "album", "camera" ],
                success: function(t) {
                    var a = t.tempFilePaths;
                    wx.compressImage({
                        src: a[0],
                        quality: 60,
                        success: function(t) {
                            var a = t.tempFilePath;
                            wx.uploadFile({
                                url: s,
                                filePath: a,
                                name: "file",
                                formData: {
                                    attach: i.data.setname
                                },
                                success: function(t) {
                                    var a = JSON.parse(t.data);
                                    if (1 == a.result) {
                                        for (var e = i.data.diydata, s = 0; s < e.length; s++) e[s].name == a.data.attach && e[s].imgUrls.push(a.data.img_url);
                                        i.setData({
                                            diydata: e
                                        });
                                    } else wx.showToast({
                                        icon: "none",
                                        title: a.msg,
                                        duration: 1500
                                    });
                                }
                            });
                        },
                        fail: function(t) {
                            console.log(t);
                        },
                        complete: function(t) {}
                    });
                }
            });
        },
        removeImg: function(t) {
            var a = t.currentTarget.dataset.indexc, e = t.currentTarget.dataset.index, i = this.data.diydata;
            i[e].imgUrls.splice(a, 1), this.setData({
                diydata: i
            });
        },
        diytextareainp: function(a) {
            var e = a.detail, i = a.currentTarget.dataset.idex;
            this.setData(t({}, "diydata[" + i + "].value.value", e));
        },
        checkboxinp: function(a) {
            var e = a.detail, i = a.currentTarget.dataset.idex;
            this.setData(t({}, "diydata[" + i + "].value", e));
        },
        diyselectBtn: function(a) {
            var e = a.currentTarget.dataset.idex;
            this.setData(t({}, "diydata[" + e + "].diysishow", !0));
        },
        raidinp: function(a) {
            var e = a.detail, i = a.currentTarget.dataset.idex;
            this.setData(t({}, "diydata[" + i + "].value", e));
        },
        showAddBtn: function(a) {
            var e = a.currentTarget.dataset.idex;
            this.setData(t({}, "diydata[" + e + "].showAdd", !0));
        },
        _closeDateLw: function(a) {
            var e = a.currentTarget.dataset.idex;
            this.setData(t({}, "diydata[" + e + "].showAdd", !1));
        },
        _initAddressInfo: function() {
            var t = this;
            wx.getStorage({
                key: "provinceData",
                success: function(a) {
                    t._getProvinceData();
                },
                fail: function(e) {
                    a._getNetWork({
                        url: a.getNetAddresss("member.member-address.address"),
                        success: function(a) {
                            var e = a.data, i = e.data.province, s = e.data.city, d = e.data.district;
                            wx.setStorage({
                                key: "provinceData",
                                data: i
                            }), wx.setStorage({
                                key: "cityData",
                                data: s
                            }), wx.setStorage({
                                key: "districtData",
                                data: d
                            }), t._initAddressInfo();
                        },
                        fail: function(t) {},
                        state: !0
                    });
                }
            });
        },
        _getProvinceData: function() {
            var t = this;
            wx.getStorage({
                key: "provinceData",
                success: function(a) {
                    t.setData({
                        provinceData: a.data
                    }), t.setData({
                        "selectAddressData[0].name": a.data[t.data.pickerValue[0]].areaname,
                        "selectAddressData[0].id": a.data[t.data.pickerValue[0]].id,
                        "selectAddressData[0].index": t.data.pickerValue[0]
                    }), t._getCityData();
                }
            });
        },
        _getCityData: function() {
            var t = this.data.selectAddressData[0], a = this;
            wx.getStorage({
                key: "cityData",
                success: function(e) {
                    for (var i = [], s = 0; s < e.data.length; s++) e.data[s].parentid == t.id && i.push(e.data[s]);
                    a.setData({
                        cityData: i
                    }), a.setData({
                        "selectAddressData[1].name": i[a.data.pickerValue[1]].areaname,
                        "selectAddressData[1].id": i[a.data.pickerValue[1]].id,
                        "selectAddressData[1].index": a.data.pickerValue[1]
                    }), a._getDistrictData();
                }
            });
        },
        _getDistrictData: function() {
            var t = this.data.selectAddressData[1], a = this;
            wx.getStorage({
                key: "districtData",
                success: function(e) {
                    for (var i = [], s = 0; s < e.data.length; s++) e.data[s].parentid == t.id && i.push(e.data[s]);
                    a.setData({
                        districtData: i
                    }), a.setData({
                        "selectAddressData[2].name": i[a.data.pickerValue[2]].areaname,
                        "selectAddressData[2].id": i[a.data.pickerValue[2]].id,
                        "selectAddressData[2].index": a.data.pickerValue[2]
                    });
                }
            });
        },
        bindChange: function(t) {
            var a = t.detail.value;
            this.data.pickerValue[0] != a[0] && (a[1] = 0, a[2] = 0), this.data.pickerValue[1] != a[1] && (a[2] = 0), 
            this.setData({
                "pickerValue[0]": a[0],
                "pickerValue[1]": a[1],
                "pickerValue[2]": a[2]
            }), this._initAddressInfo();
        },
        resultAdd: function(a) {
            var e, i = a.currentTarget.dataset.idex, s = this.data.selectAddressData, d = s[0].name + " " + s[1].name + " " + s[2].name;
            this.setData((t(e = {}, "diydata[" + i + "].value", d), t(e, "diydata[" + i + "].showAdd", !1), 
            e));
        },
        openPicker: function(a) {
            var e = a.currentTarget.dataset.idex;
            this.setData(t({}, "diydata[" + e + "].timeShow", !0));
        },
        _closeDateLw_1: function() {
            this.setData({
                showCity1: !1
            });
        },
        resultAdd_1: function() {
            var t = this.data.selectAddressData;
            this.setData({
                "applyModel.province_name": t[0].name,
                "applyModel.city_name": t[1].name,
                "applyModel.district_name": t[2].name,
                fullAddress: t[0].name + " " + t[1].name + " " + t[2].name,
                showCity1: !1
            });
        },
        submit: function() {
            this.validation(), this.data.isValidation && this.submitForm();
        },
        validation: function() {
            var t = this;
            this.setData({
                isValidation: !0
            }), this.data.diydata.forEach(function(a) {
                1 != a.data.tp_must || a.value || "diyimage" == a.type || (wx.showToast({
                    icon: "none",
                    title: a.data.tp_name + "必须填写哦",
                    duration: 1500
                }), t.setData({
                    isValidation: !1
                })), 1 == a.data.tp_must && "diyimage" == a.type && a.imgUrls.length < a.data.tp_max && (wx.showToast({
                    icon: "none",
                    title: a.data.tp_name + "必须上传" + a.data.tp_max + "张",
                    duration: 1500
                }), t.setData({
                    isValidation: !1
                })), "diycheckbox" == a.type && 0 == a.value.length && (wx.showToast({
                    icon: "none",
                    title: a.data.tp_name + "必须选择哦",
                    duration: 1500
                }), t.setData({
                    isValidation: !1
                }));
            });
        },
        submitForm: function() {
            var t = this, e = [], i = {};
            this.data.diydata.forEach(function(t) {
                "diyimage" == t.type ? i[t.name] = t.imgUrls : i[t.name] = t.value;
            }), e.push(i);
            var s = {
                form_id: this.data.datas.form_id ? this.data.datas.form_id : this.data.datas.form.id,
                form_data: e
            }, d = a.getNetAddresss("plugin.diyform.api.diy-form.save-form-data");
            a._postNetWork({
                url: d,
                showToastIn: !1,
                data: s,
                success: function(a) {
                    var e = a.data;
                    1 == e.result ? wx.showModal({
                        title: "提示",
                        content: "提交成功",
                        showCancel: !1,
                        success: function(a) {
                            a.confirm && t.getFormBack(e.data.form_id);
                        }
                    }) : wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {}
            });
        },
        showCity1btn: function() {
            this.setData({
                showCity1: !0
            });
        },
        sitemSelectBtn: function(a) {
            var e, i = a.detail.name, s = a.currentTarget.dataset.index;
            this.setData((t(e = {}, "diydata[" + s + "].value", i), t(e, "diydata[" + s + "].diysishow", !1), 
            e));
        },
        sitemClose: function(a) {
            var e = a.currentTarget.dataset.index;
            this.setData(t({}, "diydata[" + e + "].diysishow", !1));
        },
        dateClose: function(a) {
            var e = a.currentTarget.dataset.idex;
            this.setData(t({}, "diydata[" + e + "].timeShow", !1));
        },
        timestampToTime: function(t) {
            var a = new Date(t);
            return a.getFullYear() + "-" + ((a.getMonth() + 1 < 10 ? "0" + (a.getMonth() + 1) : a.getMonth() + 1) + "-") + (this.change(a.getDate()) + " ");
        },
        change: function(t) {
            return t < 10 ? "0" + t : t;
        },
        birthdayconfirm: function(a, e) {
            var i, s = a.currentTarget.dataset.idex, d = a.detail;
            this.setData((t(i = {}, "diydata[" + s + "].value", String(d).substring(0, 10)), 
            t(i, "shoTime", this.timestampToTime(d)), t(i, "diydata[" + s + "].timeShow", !1), 
            i));
        }
    }
});